import {Application} from "pinus";
import {ControlService} from "../../services/controlService";

let gameConfig:GameConfig = null

// 统一创建对象接口
export default function (app: Application) {
    if (gameConfig === null) {
        gameConfig = new GameConfig(app)
    }
    return gameConfig
}

export class GameConfig {
    private _controlService: ControlService
    private _minBet: number
    private _maxBet: number
    private _minMultiple: number
    private _maxMultiple: number
    private _gameMaxMultiple: number
    private _gameStatus: number

    constructor(private app: Application) {
        this._controlService = app.get('controlService')
    }

    public get minBet() {
        return this._minBet
    }
    public get maxBet() {
        return this._maxBet
    }
    public get minMultiple() {
        return this._minMultiple
    }
    public get maxMultiple() {
        return this._maxMultiple
    }
    public get gameMaxMultiple() {
        return this._gameMaxMultiple
    }
    public get gameStatus() {
        return this._gameStatus
    }
    

    /*
    * 初始化
    * */
    public async load(): Promise<boolean> {
        const config = await this._controlService.getGameConfig()
        if (!config) {
            return false
        }
        const control = await this._controlService.getGameControlSetting()
        if (!control){
            return false
        }
        this._minBet = config.minBet
        this._maxBet = config.maxBet
        this._minMultiple = config.minMultiple
        this._maxMultiple = config.maxMultiple
        this._gameMaxMultiple = control.gameMaxMultiple
        this._gameStatus = control.gameStatus
        return true
    }
    /*
    * 重置
    * */
    public async reload(): Promise<boolean> {
        const config = await this._controlService.getGameConfig()
        if (!config) {
            return false
        }
        this._minBet = config.minBet
        this._maxBet = config.maxBet
        this._minMultiple = config.minMultiple
        this._maxMultiple = config.maxMultiple
        return true
    }

    /*
    * 重置
    * */
    public async reloadControlSetting(): Promise<boolean> {
        const config = await this._controlService.getGameControlSetting()
        if (!config) {
            return false
        }
        this._gameMaxMultiple = config.gameMaxMultiple
        this._gameStatus = config.gameStatus
        return true
    }


}
